package com.itheima.service.mongo.impl;/*
 *      .--,       .--,
 *     ( (  \.---./  ) )
 *     '.__/o   o\__.'
 *        {=  ^  =}
 *         >  -  <
 *        /       \
 *       //       \\
 *      //|   .   |\\
 *      "'\       /'"_.-~^`'-.
 *         \  _  /--'         `
 *       ___)( )(___
 *      (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之。
 *
 * @author JiaoDaoNiu
 * @create 2022-11-11-23:40
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.PageResult;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserFindFansService;
import com.itheima.service.mongo.UserFindFansService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class UserFindFansServiceImpl implements UserFindFansService {

    @Autowired
    private MongoTemplate mongoTemplate;



    //我喜欢的  所有userLike集合
    @Override
    public List<UserLike> findMyFans(Long userId) {
        Query query=new Query(Criteria.where("userId").is(userId));
        List<UserLike> myFans = mongoTemplate.find(query, UserLike.class);
        return myFans;
    }

    //喜欢我的 所有userLike集合
    @Override
    public List<UserLike> findFansWho(Long userId) {
        return mongoTemplate.find(new Query(Criteria.where("likeUserId").is(userId)),UserLike.class);
    }


    //获取喜欢的同时也粉丝我的
    @Override
    public UserLike findByEach(Long userId, Long uid) {
        Query query=new Query(Criteria.where("userId").is(userId).and("likeUserId").is(uid));
        return mongoTemplate.findOne(query,UserLike.class);
    }

    //喜欢
    @Override
    public void save(Long userId, Long uid) {
        UserLike userLikeNew=new UserLike();
        userLikeNew.setId(new ObjectId());
        userLikeNew.setUserId(userId);
        userLikeNew.setLikeUserId(uid);
        userLikeNew.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLikeNew);

        //保存到我的好友表中去
            //看对方是否也喜欢我
        Query query=new Query(Criteria.where("userId").is(uid).and("friendId").is(userId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        if(exists) {
            Friend friend = new Friend();
            friend.setId(new ObjectId());
            friend.setUserId(userId);
            friend.setFriendId(uid);
            friend.setCreated(System.currentTimeMillis());
        }

        //把好友动态保存到我的好友动态里






    }

    //取消喜欢
    @Override
    public void delete(Long userId, Long uid) {
        Query query=new Query(Criteria.where("userId").is(userId).and("likeUserId").is(uid));
        mongoTemplate.remove(query,UserLike.class);
        //删除好友关系
        query= Query.query(Criteria.where("userId").is(userId).and("friendId").is(uid));
        mongoTemplate.remove(query,Friend.class);
    }

/*    //通过朋友表  查看相互喜欢
    @Override
    public PageBeanVo findFriendsEachPage(Long userId, Integer pageNum, Integer pageSize) {
        List<UserLike> myFans = findMyFans(userId);
        List<UserLike> fansWho = findFansWho(userId);

        if (CollUtil.isNotEmpty(myFans)) {
            for (UserLike likeMy : fansWho) {
                for (UserLike myFan : myFans) {
                    if (myFan.getLikeUserId().equals(likeMy.getUserId())) {
                        UserInfo userInfo = userInfoService.findById(myFan.getLikeUserId());
                        UserLikeVo vo = new UserLikeVo();
                        BeanUtil.copyProperties(userInfo, vo);

                        vo.setAlreadyLove(true);
                        vo.setMatchRate(RandomUtil.randomInt(100));
                        voList.add(vo);
                    }
                }
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);
        query.skip((pageNum-1)*pageSize)
                .limit(pageSize).with(Sort.by(Sort.Order.desc("created")));
//        query.with(PageRequest.of(pageNum-1,pageSize, Sort.by(Sort.Direction.DESC,"created")));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        return new PageBeanVo(pageNum,pageSize,count,friendList);
    }*/

    //访客
    @Override
    public PageBeanVo queryVisitorsList(Long userId, Integer pageNum, Integer pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Visitor.class);
        query.with(Sort.by(Sort.Order.desc("date"))).skip((pageNum-1)*pageSize).limit(pageSize);

        List<Visitor> visitorsList = mongoTemplate.find(query, Visitor.class);
        return new PageBeanVo(pageNum,pageSize,count,visitorsList);
    }
}



























